import java.awt.Color;
import java.awt.Graphics2D;


public class Projetil extends Sprite {
    public int velx = 0;
    public int vely = 0;
    
    public boolean vivo = false;
    
    Object pai = null;
    int dano = 10;
    
	float oldX = X;
	float oldY = Y;
    
    
	
	public Projetil(float x,float y,int velx,int vely,Object pai) {
		
		this.X = x;
		this.Y = y;
		
		this.velx = velx;
		this.vely = vely;
		vivo = true;
		this.pai = pai;
	}
	
	@Override
	public void SimulaSe(int diftime) {
		
		oldX = X;
		oldY = Y;
		
		float passoX = velx*diftime/1000.0f;
		float passoY = vely*diftime/1000.0f;
		
		X+=passoX;
		Y+=passoY;
		
		for(int i = 0; i < CanvasPrincipal.instance.ListaDePersonagensJogador.size();i++){
			Personagem pers = (Personagem)CanvasPrincipal.instance.ListaDePersonagensJogador.get(i);
			if(pers!=pai){
				
				float dist2 = passoX*passoX + passoY*passoY;
				
				if(dist2<9){
					if(colideCircular(pers.X,pers.Y,X,Y,pers.raio,2)){
						vivo = false;
						pers.levadano(dano);
						CanvasPrincipal.instance.addSangue(X, Y, 400, Math.atan2(oldY-Y, oldX-X));
						break;
					}
				}else{
					float dist = (float)Math.sqrt(dist2);
					int ntestes = (int)(dist/3);
					float sizepassox = passoX/ntestes;
					float sizepassoy = passoY/ntestes;
					boolean colidiu = false;
					for(int j = 1; j < ntestes;j++){
						float textx = oldX+(sizepassox*j);
						float texty = oldY+(sizepassoy*j);
						if(colideCircular(pers.X,pers.Y,textx,texty,pers.raio,2)){
							colidiu = true;
							break;
						}
					}
					if(colidiu==false){
						if(colideCircular(pers.X,pers.Y,X,Y,pers.raio,2)){
							colidiu = true;
						}
					}
					if(colidiu){
						vivo = false;
						CanvasPrincipal.instance.addSangue(X, Y, 400, Math.atan2(oldY-Y, oldX-X));
						pers.levadano(dano);
						break;
					}
				}
				
			}
		}
		
		if(X<0){
			vivo = false;
		}else if(Y<0){
			vivo = false;
		}else if(X>=((CanvasPrincipal.mapa.largura*CanvasPrincipal.mapa.tilesLargura))){
			vivo = false;
		}else if(Y>=((CanvasPrincipal.mapa.altura*CanvasPrincipal.mapa.tilesLargura))){
			vivo = false;
		}else{
			int bx = (int)((X)/CanvasPrincipal.mapa.tilesLargura);
			int by = (int)((Y)/CanvasPrincipal.mapa.tilesLargura);
			
			if(CanvasPrincipal.mapa.mapaObjetos[by][bx]>0){
				vivo = false;
			}
		}

	}

	@Override
	public void DesenhaSe(Graphics2D dbg, int xmundo, int ymundo) {


		int cor = CanvasPrincipal.instance.rnd.nextInt(3);
		
		
		if(cor == 0){
			dbg.setColor(Color.YELLOW);
		}
		if(cor == 1){
			dbg.setColor(Color.RED);
		}
		if(cor == 2){
			dbg.setColor(Color.WHITE);
		}
		//dbg.fillRect((int)(X-xmundo-2),(int)(Y-ymundo-2), 5, 5);
		dbg.drawLine((int)(X-xmundo),(int)(Y-ymundo), (int)(oldX-xmundo),(int)(oldY-ymundo));
//		dbg.setColor(Color.RED);
//		dbg.fillRect((int)(X-xmundo),(int)(Y-ymundo), 2, 2);
	}
	
	public boolean colideCircular(float x1,float y1,float x2,float y2, float r1,float r2){
		float dx = x1 - x2;
		float dy = y1 - y2;
		float d2 = dx*dx + dy*dy;
		float rsoma = (r1+r2); 
		
		if(d2<(rsoma*rsoma)){
			return true;
		}
		return false;
	}

}
